﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Com.Lancetinc.Evs.Models
{
    /// <summary>
    /// 仪器信息
    /// </summary>
    public class GenericReceivedInstrumentState : IReceivedInstrumentState
    {
        public GenericReceivedInstrumentState()
        {
            Reset();
        }

        public GenericReceivedInstrumentState(IReceivedInstrumentState instrument)
        {
            AcceptChange(instrument);
        }

        private bool? _endoscopicConnected;

        /// <summary>
        /// 内窥镜已连接？
        /// </summary>
        public bool EndoscopicConnected
        {
            get => _endoscopicConnected ?? false;
            set => _endoscopicConnected = value;
        }

        /// <summary>
        /// 耗材序列号
        /// </summary>
        public string HcSerialNo { get; set; }

        /// <summary>
        /// 耗材使用时间
        /// </summary>
        public int HcUsedTime { get; set; }

        /// <summary>
        /// 视频旋转角度
        /// </summary>
        public EnumVideoRotate VideoRotate { get; set; }

        /// <summary>
        /// LED 亮度
        /// </summary>
        public byte? LedBrightness { get; set; }

        /// <summary>
        /// 错误代码
        /// </summary>
        public byte ErrorCode { get; set; }

        public DateTime FactoryTime { get; set; }

        public virtual void Reset()
        {
            ErrorCode = 0;
            HcSerialNo = "";
            VideoRotate = EnumVideoRotate.Normal;
            LedBrightness = null;
            _endoscopicConnected = null;
            HcUsedTime = 0;
        }

        public ISet<string> AcceptChange(IReceivedInstrumentState info)
        {
            ISet<string> changedPropertyNames = new HashSet<string>();
            if (info.EndoscopicConnected != _endoscopicConnected)
            {
                changedPropertyNames.Add(PropertyConsts.EndoscopicConnected);
                EndoscopicConnected = info.EndoscopicConnected;
            }

            if (info.HcSerialNo != HcSerialNo)
            {
                changedPropertyNames.Add(PropertyConsts.HcSerialNo);
                HcSerialNo = info.HcSerialNo;
            }

            if (info.HcUsedTime != HcUsedTime)
            {
                changedPropertyNames.Add(PropertyConsts.HcUsedTime);
                HcUsedTime = info.HcUsedTime;
            }

            if (info.LedBrightness != LedBrightness)
            {
                changedPropertyNames.Add(PropertyConsts.LedBrightness);
                LedBrightness = info.LedBrightness;
            }

            if (info.VideoRotate != VideoRotate)
            {
                changedPropertyNames.Add(PropertyConsts.VideoRotate);
                VideoRotate = info.VideoRotate;
            }

            if (info.ErrorCode != ErrorCode)
            {
                changedPropertyNames.Add(PropertyConsts.ErrorCode);
                ErrorCode = info.ErrorCode;
            }

            if (info.FactoryTime != FactoryTime)
            {
                changedPropertyNames.Add(PropertyConsts.FactoryTime);
                FactoryTime = info.FactoryTime;
            }

            return changedPropertyNames;
        }

        public override string ToString()
        {
            var sb = new StringBuilder();
            sb.Append(nameof(EndoscopicConnected)).Append("=").Append(EndoscopicConnected).Append(",");
            sb.Append(nameof(HcSerialNo)).Append("=").Append(HcSerialNo).Append(",");
            sb.Append(nameof(HcUsedTime)).Append("=").Append(HcUsedTime).Append(",");
            sb.Append(nameof(VideoRotate)).Append("=").Append(VideoRotate).Append(",");
            sb.Append(nameof(LedBrightness)).Append("=").Append(LedBrightness).Append(",");
            sb.Append(nameof(ErrorCode)).Append("=").Append(ErrorCode).Append(",");
            sb.Append(nameof(FactoryTime)).Append("=").Append(FactoryTime).Append(",");
            return sb.ToString();
        }
    }
}